package com.app.bimo.library_db.helper;

import com.app.bimo.commonbusiness.util.TimeUnit;
import com.app.bimo.library_db.ReadDayRecord;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.dao.DaoSession;
import com.app.bimo.library_db.dao.ReadDayRecordDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

public class ReadDayRecordHelper {


    private static volatile ReadDayRecordHelper sInstance;
    private static DaoSession daoSession;
    private static ReadDayRecordDao recordDao;

    public static ReadDayRecordHelper getsInstance() {
        if (sInstance == null) {
            synchronized (UserHelper.class) {
                if (sInstance == null) {
                    sInstance = new ReadDayRecordHelper();
                    daoSession = DaoDbHelper.getInstance().getSession();
                    recordDao = daoSession.getReadDayRecordDao();
                }
            }
        }
        return sInstance;
    }

    /**
     * 保存阅读数据
     *
     * @param readTimeRecord
     */
    public void saveRead(ReadDayRecord readTimeRecord) {
        recordDao.insertOrReplace(readTimeRecord);
        recordDao.detach(readTimeRecord);
    }

    public void updateRead(ReadDayRecord readTimeRecord) {
        recordDao.update(readTimeRecord);
    }

    /**
     * 删除全部数据   用于退出登录时候
     */
    public void removeRecord() {
        recordDao.deleteAll();
    }

    /**
     * 移除已上报数据
     * *
     * 状态  0 未上报  1已上报   2 成功上报
     */
    public void removeUsdRecord(long time) {
        List<ReadDayRecord> list = recordDao.queryBuilder().where(ReadDayRecordDao.Properties.Status.eq(2), ReadDayRecordDao.Properties.EndTime.lt(time)).list();
        recordDao.deleteInTx(list);
    }


    /**
     * 改变数据状态
     * *
     * 状态  0 未上报  1已上报   2 成功上报
     */
    public void changeRecordStatus(int status, List<ReadDayRecord> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus(status);
        }
        recordDao.updateInTx(list);
    }


    /**
     * 查询本小说  所有未上报成功的数据
     *
     * @param
     * @return
     */
    public List<ReadDayRecord> findUnUseRecord() {
        UserData data = UserHelper.getsInstance().findUser();
        if (data == null) {
            return new ArrayList<>();
        } else {
            QueryBuilder<ReadDayRecord> qb = recordDao.queryBuilder().where(ReadDayRecordDao.Properties.Uuid.eq(data.getUuid()), ReadDayRecordDao.Properties.CreatTime.lt(TimeUnit.getTimesmorning()), ReadDayRecordDao.Properties.Status.eq(0)).orderAsc(ReadDayRecordDao.Properties.CreatTime);
            return qb.list();
        }
    }

    /**
     * 获取本周所有数据
     * 查询所有 时间大于本周开始时间的时间戳的数据   今天以前的
     *
     * @param
     * @return
     */
    private List<ReadDayRecord> getThisWeekRecords() {
        UserData data = UserHelper.getsInstance().findUser();
        if (data == null) {
            return new ArrayList<>();
        } else {
            QueryBuilder<ReadDayRecord> qb = recordDao.queryBuilder().where(ReadDayRecordDao.Properties.Uuid.eq(data.getUuid()), ReadDayRecordDao.Properties.CreatTime.gt(TimeUnit.getWeekStartTime()));
            return qb.list();
        }
    }

    /**
     * 查询本周所有阅读时长  秒数
     *
     * @param
     * @return
     */
    public long findWeekTime() {
        long times = 0;
        List<ReadDayRecord> list = getThisWeekRecords();
        if (list.size() == 0) {
            return times;
        } else {
            for (int i = 0; i < list.size(); i++) {
                ReadDayRecord dayRecord = list.get(i);
                times = times + (dayRecord.getEndTime() - dayRecord.getCreatTime());
            }
            return times;
        }
    }


}
